import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:opene/app_constant.dart';
import 'package:opene/app_utils.dart';
import 'package:opene/home/authorize_bean.dart';
import 'package:opene/net/network_manager.dart';
import 'package:opene/net/response/base_response.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../home/home.dart';
import '../me/me.dart';
import '../task/task.dart';

class MyAppMain extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Bottom Navigation Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MainPage(),
    );
  }
}

class MainPage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<MainPage> {
  int _currentIndex = 0;

  final List<Widget> _pages = [
    HomeScreen(),
    TaskScreen(),
    MeScreen(),
  ];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    grayscale();
  }

  Future<void> getJumpData() async {
    var sp = await SharedPreferences.getInstance();
    var string = sp.getString(Constants.versionClass);
    if (Utils.isEmpty(string)) {
      String jumpJson = Constants.jumpJson.replaceAll("\\\"", "\"");
      Constants.jumpMapBean = JumpMapBean.fromJson(json.decode(jumpJson));
    } else {
      Constants.jumpMapBean = JumpMapBean.fromJson(json.decode(string));
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PreferredSize(
        preferredSize: Size.zero, // 将大小设置为Size.zero来隐藏AppBar
        child: Container(
          height: 20.0, // 确保高度为0.0
          child: ColoredBox(color: Colors.transparent), // 设置背景颜色为透明
        ),
      ),
      body: _pages[_currentIndex],
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        onTap: (index) {
          print("_currentIndex${_currentIndex}");
          setState(() {
            _currentIndex = index;
          });
        },
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: 'Home',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.task),
            label: 'Tasts',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            label: 'Me',
          ),
        ],
      ),
    );
  }

  void grayscale() async {
    try {
      BaseResponse baseResponse = await NetworkManager().request(context, Constants.grayscale);

      if (baseResponse.status == 200) {
        JumpMapBean jumpMapBean = JumpMapBean.fromJson(baseResponse.data);
        if (jumpMapBean == null) {
          getJumpData();
          return;
        }
        setJumpData(jumpMapBean);
      } else {
        getJumpData();
      }
    } catch (e) {
      e.printStackTrace();
      getJumpData();
    }
  }

  Future<void> setJumpData(JumpMapBean data) async {
    String jumpJson = Constants.jumpJson.replaceAll("\\\"", "\"");
    Constants.jumpMapBean = JumpMapBean.fromJson(json.decode(jumpJson));

    if (Constants.jumpMapBean == null) {
      Constants.jumpMapBean = new JumpMapBean();
    }

    try {
      if (data.INBOUND_Checkin != null) {
        Constants.jumpMapBean.INBOUND_Checkin = (data.INBOUND_Checkin);
      }
      if (data.INBOUND_Tally != null) {
        Constants.jumpMapBean.INBOUND_Tally = (data.INBOUND_Tally);
      }
      if (data.INBOUND_Damage != null) {
        Constants.jumpMapBean.INBOUND_Damage = (data.INBOUND_Damage);
      }
      if (data.INBOUND_LPNPhoto != null) {
        Constants.jumpMapBean.INBOUND_LPNPhoto = (data.INBOUND_LPNPhoto);
      }
      if (data.INBOUND_Putaway != null) {
        Constants.jumpMapBean.INBOUND_Putaway = (data.INBOUND_Putaway);
      }
      if (data.OUTBOUND_B2C_Pickup != null) {
        Constants.jumpMapBean.OUTBOUND_B2C_Pickup = (data.OUTBOUND_B2C_Pickup);
      }
      if (data.OUTBOUND_B2C_Label != null) {
        Constants.jumpMapBean.OUTBOUND_B2C_Label = (data.OUTBOUND_B2C_Label);
      }
      if (data.OUTBOUND_B2C_OBLPN != null) {
        Constants.jumpMapBean.OUTBOUND_B2C_OBLPN = (data.OUTBOUND_B2C_OBLPN);
      }
      if (data.OUTBOUND_B2C_Trailer != null) {
        Constants.jumpMapBean.OUTBOUND_B2C_Trailer = (data.OUTBOUND_B2C_Trailer);
      }
      if (data.OUTBOUND_B2B_Pick != null) {
        Constants.jumpMapBean.OUTBOUND_B2B_Pick = (data.OUTBOUND_B2B_Pick);
      }
      if (data.OUTBOUND_B2B_B2BLabel != null) {
        Constants.jumpMapBean.OUTBOUND_B2B_B2BLabel = (data.OUTBOUND_B2B_B2BLabel);
      }
      if (data.OUTBOUND_B2B_PalletTag != null) {
        Constants.jumpMapBean.OUTBOUND_B2B_PalletTag = (data.OUTBOUND_B2B_PalletTag);
      }
      if (data.OUTBOUND_B2B_Trailer != null) {
        Constants.jumpMapBean.OUTBOUND_B2B_Trailer = (data.OUTBOUND_B2B_Trailer);
      }
      if (data.Container_Unload != null) {
        Constants.jumpMapBean.Container_Unload = (data.Container_Unload);
      }
      if (data.Container_Tally != null) {
        Constants.jumpMapBean.Container_Tally = (data.Container_Tally);
      }
      if (data.Container_Putatay != null) {
        Constants.jumpMapBean.Container_Putatay = (data.Container_Putatay);
      }
      if (data.Container_OBLPN != null) {
        Constants.jumpMapBean.Container_OBLPN = (data.Container_OBLPN);
      }
      if (data.Returns_Preprocess != null) {
        Constants.jumpMapBean.Returns_Preprocess = (data.Returns_Preprocess);
      }
      if (data.Returns_FinalProcess != null) {
        Constants.jumpMapBean.Returns_FinalProcess = (data.Returns_FinalProcess);
      }
      if (data.Returns_Putatay != null) {
        Constants.jumpMapBean.Returns_Putatay = (data.Returns_Putatay);
      }
      if (data.Returns_Trailer != null) {
        Constants.jumpMapBean.Returns_Trailer = (data.Returns_Trailer);
      }
      if (data.MANAGE_CycleCount != null) {
        Constants.jumpMapBean.MANAGE_CycleCount = (data.MANAGE_CycleCount);
      }
      if (data.MANAGE_Transfer != null) {
        Constants.jumpMapBean.MANAGE_Transfer = (data.MANAGE_Transfer);
      }
      if (data.MANAGE_Adjustment != null) {
        Constants.jumpMapBean.MANAGE_Adjustment = (data.MANAGE_Adjustment);
      }
      if (data.MANAGE_Merge != null) {
        Constants.jumpMapBean.MANAGE_Merge = (data.MANAGE_Merge);
      }
      if (data.Task_Damage != null) {
        Constants.jumpMapBean.Task_Damage = (data.Task_Damage);
      }

      String jsonStr = jsonEncode(Constants.jumpMapBean);
      var sp = await SharedPreferences.getInstance();
      sp.setString(Constants.versionClass, jsonStr);
    } catch (e) {
      e.printStackTrace();
    }
  }
}
